package org.uim.cricket.analyzer.analyze;

import java.util.List;

import org.uim.cricket.entities.MatchPlayer;
import org.uim.cricket.entities.Player;
import org.uim.cricket.entities.batting.BattingInning;
import org.uim.cricket.util.CricketFunctions;

/**
 * Includes common function which are being used by other analyze classes.
 * 
 * @version 1.0
 * @author Isuru Madushanka
 *
 */
public class AnalyzerCommonFunctions {

	private CricketFunctions cf = new CricketFunctions();
	
	/**
     * Searches a given player name in given batting list. If no player is found
     * by given name, then it will be returned -1. Otherwise it will be returned 
     * the 0-based index of the position of batting inning which player resides.
     *
     * @param list The batting inning list to be searched against.
     * @param player The player name to be searched.
     * @return The 0-based index of position in batting list. Returns -1 if no
     * player is found.
     */
    public int containInBatList(List<BattingInning> list, String player) {
    	
    	if (list == null) return -1;
        
        for (int i=0;i<list.size();i++) {
            BattingInning tmp = list.get(i);
            if (tmp == null || tmp.getBatsman() == null) continue;
            if (tmp.getBatsman().getTitleName() == null) continue;
            
            if (cf.arePlayersSame(tmp.getBatsman().getTitleName(), player)) {
                return i;
            }
        }
        
        return -1;
        
    }

    /**
     * Searches a given player in given batting list. For the search the 
     * <code>titleName</code> of the player is being taken. If no player is found
     * under <code>titleName</code>, then it will be returned -1. Otherwise it 
     * will be returned the 0-based index of the position of batting inning which 
     * player resides.
     *
     * @param list The batting inning list to be searched against.
     * @param player The player object to be searched. The <code>titleName</code>
     * will be fetched to be search against the list.
     * @return The 0-based index of position in batting list. Returns -1 if no
     * player is found.
     */
    public int containInBatList(List<BattingInning> list, Player player) {
    	
    	if (player == null || player.getTitleName() == null) return -1;
        return this.containInBatList(list, player.getTitleName());
        
    }

    public int containInMatchPlayerList(List<MatchPlayer> list, String player) {
    	if (list == null) return -1;
    	
    	for (int i=0;i<list.size();i++) {
            MatchPlayer tmp = list.get(i);
            if (tmp == null || tmp.getPlayer() == null) continue;
            if (tmp.getPlayer().getTitleName() == null) continue;
            
            if (cf.arePlayersSame(tmp.getPlayer().getTitleName(), player)) {
                return i;
            }
        }
        return -1;
        
    }
    
    public CricketFunctions getCricketFunctions() {
    	return cf;
    }
    
}
